// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  const myid = wxContext.OPENID
  const db = cloud.database()
  const _ = db.command
  const info = await db.collection('users')
  .where({
    _openid:myid
  })
  .field({
    info:true
  })
  .get()

  await db.collection('cards')
  .where({
    _id:event._id
  })
  .update({
    data:{
      comment:_.push({
        each:[{content:event.content,publishTime:event.publishTime,pinfo:info.data[0].info}]
      }),
      commentNum:_.inc(1)
    }
  })
  const id= await db.collection('cards')
  .where({
    _id:event._id
  })
  .field({
    _id:true
  })
  .get()
  const idd = id.data[0]._id
  db.collection('users')
  .where({
    _openid:myid
  })
  .update({
    data:{
      comment:_.push({
        each:[{id:idd}]
      })
    }
  })
}